import { Component, OnInit, OnDestroy } from '@angular/core';
import { Http } from '@angular/http';

import { AuthService } from '../utils/authService';

import { Router, NavigationStart, NavigationEnd, NavigationError, NavigationCancel, RoutesRecognized } from '@angular/router';


import { Util } from '../utils/util';


import { AppSettings } from '../app.settings';

import { MatDialog } from '@angular/material';

import { Dialog } from '../components/dialog/dialog';

import { Channel } from '../utils/channel';

declare var $: any;

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css'],
  providers: [AuthService,Channel]
})
export class NavbarComponent implements OnInit {


  // user
  public user: any = {};

  public source: any = {};

  public userLogin: boolean = false;

  // slideNav
  public slideNav: any = {
    side: 'over',
    opened: true,
    tooltip: true
  };

  // 用户权限路由列表
  public roleNavList: any;

  constructor(private http: Http, private userService: AuthService, private router: Router, private util: Util, private appSettings: AppSettings, public dialog: MatDialog,private channel:Channel ) {

    this.source = this.channel.init();

  }

  ngOnInit() {

    // user subscribe



    this.resize();

    $(window).on('resize', (event) => {
      event.stopPropagation();

      this.resize();
    });



    // 路由监听
    this.router.events.subscribe((event) => {


      // see also
      // console.log(val instanceof NavigationEnd);


      // 路由结束
      if (event instanceof NavigationEnd) {
        console.log(event);

        console.log(event.url);
        // 不需要验证
        if (event.url == '/info' || event.url.match('/login') || event.url.match('/reg') || event.url.match('/forget')) {
          console.log('login out ------');
          this.userLogin = false;

          

        } else {

          this.userLogin = true;

          if (this.slideNav.opened && document.body.clientWidth < 959) {
            this.slideNav.opened = false;
          }


          if (this.util.sessionStore().has('user')) {

            console.log('user router');

            let _user = this.util.sessionStore().get('user');

            if(_user.id == this.user.id){

              return;
            }

            this.user = _user;

            // 用户权限路由列表
            this.roleNavList = this.appSettings.getRoleNavList();

            console.log(this.util.sessionStore().get('user'));


            // 用户到期提醒续费
            this._modal_deadLine(this.user.activeVersionInfo.deadLine);

          }


        }
      }
    });

  }

  ngOnDestroy() {
    $(window).off('resize');

  }


  slideNavHandel() {
    if (document.body.clientWidth < 960) {
      this.slideNav.opened = !this.slideNav.opened;
    }

  }

  // 屏幕分辨率
  resize() {
    if (document.body.clientWidth < 960) {

      this.slideNav.mode = 'over';
      this.slideNav.opened = false;

    } else {

      if (!this.slideNav.opened) {

        this.slideNav.mode = 'side';
        this.slideNav.opened = true;
      }
      // tooltip
      if (document.body.clientWidth < 1400) {

        this.slideNav.tooltip = false;

      } else {

        this.slideNav.tooltip = true;
      }
    }
  }

  // 续费
  shopping() {
   
  }

  // 退出
  logOut() {
    
  }



  //倒计时进度条
  datetime = {
    width: '100%'
  };
  //账户到期提醒
  _modal_deadLine(time) {
    // alert(0);

    // console.log(time);

    let remainderMillisecondsForDay = new Date(time).getTime() - new Date().getTime();


    let day = Math.floor(remainderMillisecondsForDay / (24 * 60 * 60 * 1000));

    let remainderMillisecondsForHour = remainderMillisecondsForDay - (day * 24 * 60 * 60 * 1000);
    let hour = Math.floor(remainderMillisecondsForHour / (60 * 60 * 1000));

    // var remainderMillisecondsForMinute = remainderMillisecondsForHour - (hour * 60 * 60 * 1000);

    // var minutes = Math.floor(remainderMillisecondsForMinute / (60 * 1000));

    // 倒计时
    this.datetime.width = (day / 365 * 100) + '%';


    if (day > 15) {
      return;
    }
    if ((day <= 5 || day == 15) && !this.util.store().has('expiration_remind_' + day + '_days')) {
      let modal = {
        title: '账号到期提醒',
        content: {
          text: '<span>您的账号距离到期剩余' + day + '天' + hour + '小时,请您及时续费!。</span>'
        }
      };

      setTimeout(() => {
        this.dialog.open(Dialog, {
          data: {
            title: '账号到期提醒',
            body: '<span>您的账号距离到期剩余' + day + '天' + hour + '小时,请您及时续费!。</span>',
            btn: false
          }
        });
      }, 3000);


      this.util.store().set('expiration_remind_' + day + '_days', 'true');
    }


  }




  // 渠道商
  pageInit(): void {

    let el: any = document.querySelector('link[rel=icon]');

    if (window.location.href.indexOf('shop.630game.com') != -1) {

      document.title = "电商先生";

      this.user.logoName = "电商先生";



      el.href = "http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/icon.png";


    } else if (window.location.href.indexOf('esm.fenxiao8.com') != -1) {

      document.title = "品谱大数据";

      this.user.logoName = "品谱大数据";

      el.href = "http://www.hzbigdate.com/assets/img/pingpu.png";



    } else if (window.location.href.indexOf('hzbigdate.com') != -1) {

      document.title = "品谱大数据";

      this.user.logoName = "品谱大数据";

      el.href = "./assets/img/ppdsj/pingpu.png";



    } else if (window.location.href.indexOf('qzt.sinoac.org') != -1) {

      document.title = "企智通";

      this.user.logoName = "企智通";

      el.href = "./assets/img/qzt/qzt_ico.png";



    } else if (window.location.href.indexOf('shuju.315taokong.com') != -1) {

      document.title = "淘控";

      this.user.logoName = "淘控";

      el.href = "./assets/img/tkds/logo.jpg";



    } else if (window.location.href.indexOf('info.veiwa.cn') != -1) {

      document.title = "数据空间";

      this.user.logoName = "数据空间";

      el.href = "./assets/img/sjkj/logo.png";



    } else {
      document.title = "情报魔方";
      this.user.logoName = "情报魔方";


      el.href = 'http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/favicon.ico';


    }
  }
}
